1423B - Valuable Paper - CodeForces Solution


binary search flows graph matchings graphs *1900

Please click on ads to support us..

C++ Code:

//二分最大边,加上网络流跑二分图匹配
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10,M=1e5+10,inf=1<<25;
struct Rode
{
    int u,v,t;
}rode[M];
int head[N*3],cent;
int n,m;//点数和边数
int maxflow;
int dep[N*3],ans[N*3],vir[N*3];
struct Node
{
    int ui,vi;
    int ti;
    int next;
}node[M*4];
void add(int u,int v,int t)
{
    node[cent].vi=v;
    node[cent].ti=t;
    node[cent].next=head[u];
    head[u]=cent++;
}
bool bfs()
{//分层
    for(int i=0;i<=2*n+1;i++)
    {
        dep[i]=0x3f3f3f3f;
        ans[i]=0;
        vir[i]=head[i];
    }
    queue<int>q;
    q.push(0);
    dep[0]=0;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        ans[now]=0;
        for(int i=head[now];~i;i=node[i].next)
        {
            int to=node[i].vi;
            if(dep[to]>dep[now]+1&&node[i].ti)
            {
                dep[to]=dep[now]+1;
                if(ans[to]==0)
                {
                    ans[to]=1;
                    q.push(to);
                }
            }
        }
    }
    if(dep[2*n+1]!=0x3f3f3f3f) return 1;
    return 0;
}
int dfs(int u,int flow)
{
    int rlow=0;
    if(u==2*n+1)//如果到达了终点
    {
        maxflow+=flow;
        return flow;
    }
    int used=0;
    for(int i=vir[u];~i;i=node[i].next)
    {
        vir[u]=i;
        int d=node[i].vi;
        if(node[i].ti&&dep[d]==dep[u]+1)
        {
            if(rlow=dfs(d,min(flow-used,node[i].ti)))
            {
                used+=rlow;
                node[i].ti-=rlow;
                node[i^1].ti+=rlow;
                if(used==flow) break;
            }
        }
    }
    return used;
}
int Dinic()
{
    while(bfs())
    {
        dfs(0,inf);
    }
    return maxflow;
}
int check(int f)
{
    memset(head,-1,sizeof(head));
    cent=0;
    for(int i=1;i<=n;i++)
    {
        add(0,i,1);//将0作为超级源点,2*n+1作为超级汇点
        add(i,0,0);
        add(n+i,2*n+1,1);
        add(2*n+1,n+i,0);
    }
    for(int i=1;i<=m;i++)
    {
        if(rode[i].t<=f)
        {//如果这条边满足要求
            add(rode[i].u,rode[i].v+n,1);
            add(rode[i].v+n,rode[i].u,0);
        }
    }
    maxflow=0;
    Dinic();
    if(maxflow<n) return 0;
    else return 1; 
}
int main()
{
    scanf("%d%d",&n,&m);
    int minn=inf,maxn=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&rode[i].u,&rode[i].v,&rode[i].t);
        if(rode[i].t<minn) minn=rode[i].t;
        if(rode[i].t>maxn) maxn=rode[i].t;
    }
    int l=minn,r=maxn;
    int res=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            res=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",res);
    //system("pause");
    return 0;
}


Comments

Submit
0 Comments
More Questions

1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure